package com.fingard.dsp.bank.directbank.abc24;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 农行特色支付渠道单笔支付
 * 
 * 参考文档:  第三方机构转账接口说明书V1.1-ABC.doc
 * 开发接口：
 * 			1、3.1 转账
 * 			2、3.2 查询
 * 
 * @author Cruise
 * @date 2019/3/20
 */
public class ABC24SinglePay extends ABC24Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		
		tmpRetDetail = d_190802(p_req1908);
		if(!"0".equals(tmpRetDetail.respCode) && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)){
			tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
	
	public RetAtsSglDetail d_190802(ReqSingle reqSingle) throws Exception {
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();

		try {
			ABC24Header tmpBankHeader = new ABC24Header();
			tmpBankHeader.transCode = ABC24TransCode.singlePay_1908_190802;
			tmpBankHeader.reqSeqNo = reqSingle.singleDetail.askNumber;
			
			String payerbkno = this.getBankConfig().getMapValueByKey("payerbknoMapping", reqSingle.singleDetail.payAct);

			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHead(xmlWriter);
			
			XElement tmpXElement = new XElement("ReqMsg", "");
			tmpXElement.addChild(new XElement("TxnType", tmpBankHeader.transCode));
			tmpXElement.addChild(new XElement("ReqSeqNo", tmpBankHeader.reqSeqNo));
			tmpXElement.addChild(new XElement("SpId", bankFront.corpNo));
			tmpXElement.addChild(new XElement("ReqDate", Format.Date8Format.format(reqSingle.singleDetail.getPayDateTime())));
			tmpXElement.addChild(new XElement("ReqTime", Format.Time6Format.format(reqSingle.singleDetail.getPayDateTime())));
			tmpXElement.addChild(new XElement("amt", reqSingle.singleDetail.payAmount));
			tmpXElement.addChild(new XElement("payerbkno", payerbkno));
			tmpXElement.addChild(new XElement("payaccno", reqSingle.singleDetail.payAct));
			tmpXElement.addChild(new XElement("payname", reqSingle.singleDetail.payName));
			tmpXElement.addChild(new XElement("payaddr", ""));
			tmpXElement.addChild(new XElement("reccnapsno", reqSingle.singleDetail.cnapsCode));
			tmpXElement.addChild(new XElement("recaccno", reqSingle.singleDetail.recAct));
			tmpXElement.addChild(new XElement("recname", reqSingle.singleDetail.recName));
			tmpXElement.addChild(new XElement("recaddr", ""));
			
			//N:农行；T：他行
			String sameBnk = reqSingle.singleDetail.sameBnk;
			if ("1".equals(sameBnk)) {
				sameBnk = "N";
			} else {
				sameBnk = "T";
			}
			tmpXElement.addChild(new XElement("flag", sameBnk));
			
			//G:对公；S：个人
			String isPrivate = reqSingle.singleDetail.isPrivate;
			if ("1".equals(isPrivate)) {
				isPrivate = "S";
			} else {
				isPrivate = "G";
			}
			tmpXElement.addChild(new XElement("sign", isPrivate));
			tmpXElement.addChild(new XElement("addtlinf", reqSingle.singleDetail.askNumber));
			nodeAnalysis(reqSingle.singleDetail.srcXElement, tmpXElement, getCharset());
			xmlWriter.writeXElement(tmpXElement);
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String bkRootName = "ResMsg";
					if(tmpStrRet[1].contains(bkRootName)==false && tmpStrRet[1].contains("ReqMsg")){
						bkRootName = "ReqMsg";
					}
					String tmpRespCode = tmpBkReader.getFirstText("/"+bkRootName+"/respCode");
					String tmpRespInfo = tmpBkReader.getFirstText("/"+bkRootName+"/respText");
					String bkReqSeqNo = tmpBkReader.getFirstText("/"+bkRootName+"/ReqSeqNo");
					if(bkReqSeqNo.equals(reqSingle.singleDetail.askNumber)){
						tmpRetDetail.reqSeqID = bkReqSeqNo;
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpRespInfo;
						tmpRetDetail.transState = tmpRespCode;
						if("9999".equals(tmpRespCode)){
							tmpRetDetail.transState = tmpRespCode+"+"+tmpRespInfo;
						}
						tmpRetDetail.payInfoCode = tmpRespCode;
					}else{
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = "银行响应流水号"+bkReqSeqNo+"与请求"+reqSingle.singleDetail.askNumber+"不一致";
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			}else if (ConnState.FAIL.equals(tmpStrRet[0])) {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = tmpStrRet[1];
				tmpRetDetail.transState = PaymentState.FAIL_Payment;
				tmpRetDetail.payInfoCode = tmpStrRet[0];
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return tmpRetDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req1944.askNumber;
		
		try {
			ABC24Header tmpBankHeader = new ABC24Header();
			tmpBankHeader.transCode = ABC24TransCode.singlePayQuery_1944_190802;
			tmpBankHeader.reqSeqNo = p_req1944.reqHeader.transSeqID;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHead(xmlWriter);
			
			XElement tmpXElement = new XElement("ReqMsg", "");
			tmpXElement.addChild(new XElement("TxnType", tmpBankHeader.transCode));
			tmpXElement.addChild(new XElement("ReqSeqNo", tmpBankHeader.reqSeqNo));
			tmpXElement.addChild(new XElement("SpId", bankFront.corpNo));
			Date tmpDateNow = new Date();
			tmpXElement.addChild(new XElement("ReqDate", Format.Date8Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("ReqTime", Format.Time6Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("YReqSeqNo", p_req1944.askNumber));
			xmlWriter.writeXElement(tmpXElement);
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String bkRootName = "ResMsg";
					if(tmpStrRet[1].contains(bkRootName)==false && tmpStrRet[1].contains("ReqMsg")){
						bkRootName = "ReqMsg";
					}
					String tmpRespCode = tmpBkReader.getFirstText("/"+bkRootName+"/respCode");
					String tmpRespInfo = tmpBkReader.getFirstText("/"+bkRootName+"/respText");
					String bkReqSeqNo = tmpBkReader.getFirstText("/"+bkRootName+"/ReqSeqNo");
					if(bkReqSeqNo.equals(tmpBankHeader.reqSeqNo)){
						tmpRetDetail.reqSeqID = p_req1944.askNumber;
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpRespInfo;
						tmpRetDetail.transState = tmpRespCode;
						tmpRetDetail.payInfoCode = tmpRespCode;
						if("9999".equals(tmpRespCode)){
							tmpRetDetail.transState = tmpRespCode+"+"+tmpRespInfo;
						}
					}else{
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = "银行响应流水号"+bkReqSeqNo+"与请求"+tmpBankHeader.reqSeqNo+"不一致";
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
